Skip to content

Fix tests rendering UndeclaredThrowableException instead of InvalidDataAccessApiUsageException#2290

Merged
EgorkaKulikov merged 1 commit into
mainfrom
ilya_m/fix_undeclared_throwable_from_repository
Jun 19, 2023
Merged

Fix tests rendering UndeclaredThrowableException instead of InvalidDataAccessApiUsageException#2290
EgorkaKulikov merged 1 commit into
mainfrom
ilya_m/fix_undeclared_throwable_from_repository

Conversation

@IlyaMuravjov

Copy link
Copy Markdown
Collaborator

Description

When exception was thrown by any Repository method then RepositoryWrapperInvocationHandler was causing it to get wrapped into InvocationTargetException, leading to tests with comments rendering incorrect stack trace:

@Test
@DisplayName("createOrder: order = null -> throw UndeclaredThrowableException")
public void testCreateOrderThrowsUTE() {
    OrderService orderService = ((OrderService) applicationContext.getBean("orderService"));
    
    /* This test fails because method [com.rest.order.services.OrderService.createOrder] produces [java.lang.reflect.UndeclaredThrowableException]
        com.sun.proxy.$Proxy147.save(Unknown Source)
        com.rest.order.services.OrderService.createOrder(OrderService.java:52) */
    orderService.createOrder(null);
}

How to test

Manual tests

Generate tests for spring-boot-testing-main, failed tests should render correct stack trace like this one:

@Test
@DisplayName("createOrder: order = null -> throw InvalidDataAccessApiUsageException")
public void testCreateOrderThrowsIDAAUE1() {
    OrderService orderService = ((OrderService) applicationContext.getBean("orderService"));
    
    /* This test fails because method [com.rest.order.services.OrderService.createOrder] produces [org.springframework.dao.InvalidDataAccessApiUsageException: Entity must not be null.; nested exception is java.lang.IllegalArgumentException: Entity must not be null.]
        org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:374)
        org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:235)
        org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:551)
        org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
        org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
        org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:152)
        org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
        org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:174)
        org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
        org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
        org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
        org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)
        com.sun.proxy.$Proxy147.save(Unknown Source)
        jdk.internal.reflect.GeneratedMethodAccessor53.invoke(Unknown Source)
        java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        java.base/java.lang.reflect.Method.invoke(Method.java:566)
        org.utbot.spring.repositoryWrapper.RepositoryWrapperInvocationHandler.invoke(RepositoryWrapperInvocationHandler.kt:15)
        com.sun.proxy.$Proxy147.save(Unknown Source)
        com.rest.order.services.OrderService.createOrder(OrderService.java:52) */
    orderService.createOrder(null);
}

Self-check list

  • I've set the proper labels for my PR (at least, for category and component).
  • PR title and description are clear and intelligible.
  • I've added enough comments to my code, particularly in hard-to-understand areas.
  • The functionality I've repaired, changed or added is covered with automated tests.
  • Manual tests have been provided optionally.
  • The documentation for the functionality I've been working on is up-to-date.

…dler` to show stack trace in generated tests
@IlyaMuravjov IlyaMuravjov added ctg-bug-fix PR is fixing a bug comp-spring Issue is related to Spring projects support labels Jun 16, 2023
@EgorkaKulikov EgorkaKulikov merged commit af3aab5 into main Jun 19, 2023
@EgorkaKulikov EgorkaKulikov deleted the ilya_m/fix_undeclared_throwable_from_repository branch June 19, 2023 06:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-spring Issue is related to Spring projects support ctg-bug-fix PR is fixing a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants